/*
 * 
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.showcase.client.page;

import java.util.Arrays;
import java.util.List;

import com.cleangwt.bootstrap.client.style.Btn;
import com.cleangwt.bootstrap.client.ui.CheckField;
import com.cleangwt.bootstrap.client.ui.GroupFormPanel;
import com.cleangwt.bootstrap.support.client.TooltipErrorHandler;
import com.cleangwt.page.client.PageContext.CodeSplit;
import com.cleangwt.page.client.PageContext.Page;
import com.cleangwt.page.client.PageContext.Prefetch;
import com.cleangwt.showcase.client.action.GroupValidatorAction;
import com.cleangwt.showcase.client.component.ShowcasePage;
import com.cleangwt.showcase.client.resource.PageGroups;
import com.cleangwt.showcase.client.resource.i18n.ShowcaseMessages;
import com.cleangwt.showcase.client.resource.i18n.ValidatorMessages;
import com.cleangwt.uibinder.client.RequiresFormRebind;
import com.cleangwt.uibinder.client.UiInput;
import com.cleangwt.user.client.ui.Button;
import com.cleangwt.user.client.ui.DateBox;
import com.cleangwt.user.client.ui.Lay;
import com.cleangwt.user.client.ui.NumberBox;
import com.cleangwt.user.shared.Option;
import com.cleangwt.validation.client.Validator;
import com.cleangwt.validation.client.constraints.Max;
import com.cleangwt.validation.client.constraints.Min;
import com.cleangwt.validation.client.constraints.NotNull;
import com.cleangwt.validation.client.constraints.relative.Ge;
import com.google.gwt.core.client.GWT;

/**
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
@Prefetch(PageGroups.VALIDATION)
@CodeSplit
@Page
public abstract class GroupValidationDemo extends ShowcasePage implements RequiresFormRebind {

  public ValidatorMessages c = GWT.create(ValidatorMessages.class);

  @UiInput
  @NotNull(groups = "required")
  DateBox datbBegin1 = new DateBox();
  @NotNull(groups = "required")
  @Ge(field = "datbBegin1", name1 = "End Date1", name2 = "Begin Date1", groups = "period")
  @UiInput
  DateBox datbEnd1 = new DateBox();
  @NotNull(groups = "required")
  @Ge(field = "datbEnd1", name1 = "Begin Date2", name2 = "End Date1", groups = "period")
  @UiInput
  DateBox datbBegin2 = new DateBox();
  @NotNull(groups = "required")
  @Ge(field = "datbBegin2", name1 = "End Date2", name2 = "Begin Date2", groups = "period")
  @UiInput
  DateBox datbEnd2 = new DateBox();
  @UiInput
  @Min(1)
  @Max(100)
  NumberBox numf1 = new NumberBox();
  @UiInput
  @Min(1)
  @Max(100)
  @Ge(field = "numf1", name1 = "Number2", name2 = "Number1")
  NumberBox numf2 = new NumberBox();

  public CheckField chkfValidateGroup = new CheckField();
  public Button btnVerify = new Button(c.verify());
  public Validator validator = new Validator(new TooltipErrorHandler());

  public GroupValidationDemo() {
    super(ShowcaseMessages.Impl.groupvalidation(),
        "For complex constraints, set constraints with the 'groups'", "GroupValidationDemo.java");
    GroupFormPanel form = new GroupFormPanel();
    setContentWidget(form);
    form.build().add("Period", Lay.hor(datbBegin1, datbEnd1));
    form.build().add("Period", Lay.hor(datbBegin2, datbEnd2));
    form.build().add("Range", Lay.hor(numf1, numf2));
    form.build().add("Validate Groups", chkfValidateGroup);
    form.build().add("(The default groups must be validated)", Btn.primary(btnVerify));

    chkfValidateGroup.setOptions(getValidateGroups());
    // btnVerify.setStyleBase(Buttons.PRIMARY_STYLE);
    new GroupValidatorAction().addEventHandlers(this);
    this.addConstraints(validator);
    // activate
    validator.activate();
  }

  private List<Option> getValidateGroups() {
    return Arrays.asList(new Option("With Required Group", "required"), new Option(
        "With Period Group", "period"));
  }

}
